package us.icitap.timsportal.management;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import us.icitap.timsportal.entity.Module;
import us.icitap.timsportal.entity.Role;
import us.icitap.timsportal.resources.TimsPortalLabels;
import us.icitap.timsportal.web.custom.PortalForm;
import us.icitap.timsportal.web.custom.PortalPanel;

import com.vaadin.data.Item;
import com.vaadin.data.util.HierarchicalContainer;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.ui.Tree;

public class RoleComponent extends PortalForm{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private final Tree tree = new Tree();
	private HierarchicalContainer container;
	private HashSet<Role> roles = new HashSet<Role>();
	private final HashSet<Module> modules = new HashSet<Module>();
	private final HashSet<Role> temp = new HashSet<Role>();
	
	public RoleComponent(){
		
		tree.addStyleName("checkboxed");
		tree.setSelectable(false);
		tree.setImmediate(true);

		tree.setItemCaptionMode(Tree.ITEM_CAPTION_MODE_PROPERTY);
		tree.setItemCaptionPropertyId("caption");
		
		tree.addListener(new ItemClickEvent.ItemClickListener() {			

			private static final long serialVersionUID = 1L;

			@Override
			public void itemClick(ItemClickEvent event) {
				System.out.println("click");
				// TODO Auto-generated method stub
				Object o = event.getItemId();
				if(o.getClass() == Module.class){
					Module m = (Module)o;
					if(modules.contains(m)){
						modules.remove(m);
						roles.removeAll(m.getRoles());
					} else {
						modules.add(m);
						roles.addAll(m.getRoles());
					}									
				} else if(o.getClass() == Role.class){
					Role r = (Role) o;
					if(roles.contains(r)){
						roles.remove(r);
						temp.clear();
						temp.addAll(roles);
						temp.retainAll(r.getModule().getRoles());
						if(temp.size()==0)
							modules.remove(r.getModule());
					}
					else {
						roles.add(r);
						if(!modules.contains(r.getModule()))
							modules.add(r.getModule());
					}
						
				}
				tree.requestRepaint();
			}
		});
		
		Tree.ItemStyleGenerator itemStyleGenerator = new Tree.ItemStyleGenerator() {
			private static final long serialVersionUID = -401201653443233243L;

			@Override
			public String getStyle(Object itemId) {
				
				if(itemId.getClass() == Module.class){
					Module m = (Module)itemId;
					if(modules.contains(m))
						return "checked";
					else
						return "unchecked";
				} else if(itemId.getClass() == Role.class){
					Role r = (Role) itemId;
					if(roles.contains(r))
						return "checked";
					else
						return "unchecked";
				} else return null;
			}
		};
		tree.setItemStyleGenerator(itemStyleGenerator);
		
		PortalPanel panel = new PortalPanel(TimsPortalLabels.getLabel("label.roles"));
		panel.setWidth("300px");
		panel.addComponent(tree);
		getLayout().addComponent(panel);
	}
	
	@SuppressWarnings("rawtypes")
	public void setRoles(Set<Role> roles){
		modules.clear();
		HashSet<Role> r = new HashSet<Role>();
		r.addAll(roles);
		this.roles = r;
		Iterator iterator = r.iterator();
		while(iterator.hasNext())
			modules.add(((Role)iterator.next()).getModule());
		tree.requestRepaint();
	}
	
	public HashSet<Role> getRoles(){
		return this.roles;
	}
	
	
	@Override
	public void attach(){
		
		List<Module> list = getApplication().getPortalBean().getListModule(true);
		container = new HierarchicalContainer();
		container.addContainerProperty("caption", String.class, "");
		
		for(Module m: list){
			container.addItem(m);
			container.getItem(m).getItemProperty("caption").setValue(m.getModuleName());
			for(Role r:m.getRoles()){
				Item item = container.addItem(r);
				container.setChildrenAllowed(r, false);
				item.getItemProperty("caption").setValue(r.getTag());
				container.setParent(r, m);
			}
		}
		tree.setContainerDataSource(container);
		
	}

}
